package com.sleep.psy.external_server.business.resourse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sleep.psy.external_server.business.resourse.entity.QuestMain;
import com.sleep.psy.external_server.business.resourse.entity.ResourseScale;
import com.sleep.psy.external_server.business.resourse.entity.dto.Scale;
import com.sleep.psy.external_server.business.resourse.entity.vo.ScaleReq;
import com.sleep.psy.external_server.business.resourse.mapper.ResourseScaleCategoryReleMapper;
import com.sleep.psy.external_server.business.resourse.mapper.ResourseScaleMapper;
import com.sleep.psy.external_server.business.resourse.service.QuestMainService;
import com.sleep.psy.external_server.business.resourse.service.ResourseScaleService;
import com.sleep.psy.external_server.constants.Constant;
import com.sleep.psy.external_server.constants.MySqlConstant;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sleep.psy.external_server.result.Result;
import com.sleep.psy.external_server.result.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 量表资源 服务实现类
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-12
 */
@Service
public class ResourseScaleServiceImpl extends ServiceImpl<ResourseScaleMapper, ResourseScale> implements ResourseScaleService {

    protected Logger logger = LoggerFactory.getLogger(ResourseScaleService.class);

    @Autowired
    private ResourseScaleMapper resourseScaleMapper;

    @Autowired
    private ResourseScaleCategoryReleMapper resourseScaleCategoryReleMapper;

    @Autowired
    private QuestMainService questMainService;


    /**
     * 获取量表
     * @author wenliguo
     * @date 2025/6/5 16:28
     * @param req 请求参数
     * @return com.sleep.psy.main_server.result.Result
     */
    @Override
    public Result selectScale(ScaleReq req) {

        long pageNum = Constant.PAGE_NUM;
        long pageSize = Constant.PAGE_SIZE;

        if (null != req.getPageNum()) {
            pageNum = req.getPageNum();
        }
        if (null != req.getPageSize()) {
            pageSize = req.getPageSize();
        }
        IPage<Scale> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<Scale> iPage = resourseScaleMapper.selectScaleByCode(page, req.getCode());

        if (!iPage.getRecords().isEmpty()) {
            for (Scale record : iPage.getRecords()) {
                if (MySqlConstant.SOURCE_FLAG_3.equals(record.getSourceFlag())) {
                    if (null != record.getCuId()) {
                        if (619 == record.getCuId()) {
                            List<QuestMain> mains = questMainService.selectByType(req.getQuestType());
                            record.setQuestMainList(mains);
                        }
                    }
                }
            }
        }

        return ResultUtil.success(iPage);
    }

    /**
     * 通过量表id获取量表
     * @author wenliguo
     * @date 2025/8/13 09:38
     * @param scaleId 量表id
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectScaleById(Integer scaleId) {
        ResourseScale scale = resourseScaleMapper.selectById(scaleId);
        if (null != scale) {
            if (MySqlConstant.SOURCE_FLAG_3.equals(scale.getSourceFlag())) {
                if (null != scale.getCuId()) {
                    if (619 == scale.getCuId()) {
                        List<QuestMain> mains = questMainService.selectByType("1,2,3,4,5,6");
                        scale.setQuestMainList(mains);
                    }
                }
            }
        }

        return ResultUtil.success(scale);
    }

    /**
     * 通过ids查询量表
     * @author wenliguo
     * @date 2025/8/13 15:58
     * @param ids ids
     * @return java.util.List<com.sleep.psy.external_server.resourse.entity.ResourseScale>
     */
    @Override
    public List<ResourseScale> selectScalesByIds(List<String> ids) {
        QueryWrapper<ResourseScale> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids)
                .eq("status", MySqlConstant.STATUS_1);
        return resourseScaleMapper.selectList(wrapper);
    }

    /**
     * 获取自助训练量表
     * @author wenliguo
     * @date 2025/8/28 11:22
     * @param pageNum 当前页
     * @param pageSize 每页显示条数
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectAutoTraingScales(Long pageNum, Long pageSize) {
        IPage<Scale> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<Scale> iPage = resourseScaleMapper.selectAutoTraingScales(page);

        if (!iPage.getRecords().isEmpty()) {
            for (Scale record : iPage.getRecords()) {
                if (MySqlConstant.SOURCE_FLAG_3.equals(record.getSourceFlag())) {
                    if (null != record.getCuId()) {
                        if (619 == record.getCuId()) {
                            List<QuestMain> mains = questMainService.selectByType("1,2,3,4,5,6");
                            record.setQuestMainList(mains);
                        }
                    }
                }
            }
        }

        return ResultUtil.success(iPage);

    }
}
